//
//  LZSMyViewController.m
//  IOSProject
//
//  Created by Mac on 2018/4/16.
//  Copyright © 2018年 com.linzhisheng. All rights reserved.
//

#import "LZSMyViewController.h"

#import "SXTMyTableViewCell.h"
#import "SXTMyHeadView.h"//我的页面顶部注册登陆view
#import "SXTMyTableView.h"//我的功能列表
#import "SXTLandingViewController.h"//注册页面
#import "SXTLoginViewController.h"//登录页面
#import <Masonry.h>

#import "LZSCollectionViewController.h"
#import "LZSOptionViewController.h"
#import "LZSAboutViewController.h"
#import "LZSSetUpViewController.h"

@interface LZSMyViewController ()<UIWebViewDelegate>
@property (strong, nonatomic)   SXTMyHeadView *headView;              /** 顶部现实注册登录按钮的view */

@property (strong, nonatomic)   SXTMyTableView *myTableView;              /** 我的功能模块 */
@end

@implementation LZSMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = MainColor;
    self.edgesForExtendedLayout = 0;
    [self.view addSubview:self.myTableView];
    
    //
//    [self setupNav];
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:@"LOGINSUCCESS" object:nil];
}

-(void)setupNav{
    UIButton *presentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [presentBtn setTitle:@"界面切换" forState:UIControlStateNormal];
    [presentBtn setImage:[UIImage imageNamed:@"设置.png"] forState:UIControlStateNormal];
    [presentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    presentBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [presentBtn addTarget:self action:@selector(PresentBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:presentBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

-(void)PresentBtnClick:(UIButton *)btn{
    LZSSetUpViewController *setvc = [[LZSSetUpViewController alloc] init];
    [self.navigationController pushViewController:setvc animated:YES];
}

-(void)loginSuccess{
    [self.headView reloadHeadView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_myTableView reloadData];
    [_headView reloadHeadView];
}

- (SXTMyHeadView *)headView{
    if (!_headView) {
        _headView = [[SXTMyHeadView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 160)];
        __weak typeof (self) weakSelf = self;
//        _headView.landingBlock = ^(){
//            SXTLandingViewController *landingView = [[SXTLandingViewController alloc]init];
//            [weakSelf.navigationController pushViewController:landingView animated:YES];
//        };
        _headView.loginBlock = ^(){
            SXTLoginViewController *loginView = [[SXTLoginViewController alloc]init];
            [weakSelf.navigationController pushViewController:loginView animated:YES];
        };
        
        [_headView reloadHeadView];
    }
    return _headView;
}

- (SXTMyTableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[SXTMyTableView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) style:(UITableViewStylePlain)];
        __weak typeof (self) weakSelf = self;
        _myTableView.exitBlock = ^(){
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您是否退出当前账号" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"ISLOGIN"];
                //发布一个退出登录的通知
                [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGOUT" object:nil];
                [weakSelf.myTableView reloadData];
                [weakSelf.headView reloadHeadView];
            }];
            UIAlertAction *cancelalert = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alertVC addAction:okAction];
            [alertVC addAction:cancelalert];
            
            [weakSelf presentViewController:alertVC animated:YES completion:nil];
        };
        
        _myTableView.selectedBlock = ^(NSInteger index) {
            [weakSelf switchselectedIndex:index];
        };
        _myTableView.tableHeaderView = self.headView;
    }
    return _myTableView;
}

-(void)switchselectedIndex:(NSInteger)index{
    NSDictionary *loginDic = [[NSUserDefaults standardUserDefaults]valueForKey:@"ISLOGIN"];
    switch (index) {
        case 0:{
            if (loginDic.count) {
                //我的收藏
                LZSCollectionViewController *collectionVC = [[LZSCollectionViewController alloc] init];
                [self.navigationController pushViewController:collectionVC animated:YES];
            }else{
                
                UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"亲,您还未登录,暂时无法使用该功能~" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *okaction = [UIAlertAction actionWithTitle:@"去登陆" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    SXTLoginViewController *loginVc = [[SXTLoginViewController alloc] init];
                    [self.navigationController pushViewController:loginVc animated:YES];
                }];
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                
                [alertVC addAction:okaction];
                [alertVC addAction:cancelAction];
                [self presentViewController:alertVC animated:YES completion:nil];
            }
        }
            break;
        case 1:{
            if (loginDic.count) {
                //意见反馈
                LZSOptionViewController *OptionVC = [[LZSOptionViewController alloc] init];
                [self.navigationController pushViewController:OptionVC animated:YES];
            }else{
                
                UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"亲,您还未登录,暂时无法使用该功能~" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *okaction = [UIAlertAction actionWithTitle:@"去登陆" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    SXTLoginViewController *loginVc = [[SXTLoginViewController alloc] init];
                    [self.navigationController pushViewController:loginVc animated:YES];
                }];
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                
                [alertVC addAction:okaction];
                [alertVC addAction:cancelAction];
                [self presentViewController:alertVC animated:YES completion:nil];
            }
        }
            break;
        case 2:{
            //关于我们
            LZSAboutViewController *aboutVC = [[LZSAboutViewController alloc] init];
            [self.navigationController pushViewController:aboutVC animated:YES];
        }
            break;
        case 3:{
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"400-100-1111"];
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                
            });
        }
            break;
        case 4:{
            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
                UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
                NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",@"123456"]];
                
                NSURLRequest *request = [NSURLRequest requestWithURL:url];
                webView.delegate = self;
                [webView loadRequest:request];
                [self.view.superview addSubview:webView];
            }else{
                
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"对不起，您还没安装QQ" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    return ;
                }];
                
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
            break;
            
        default:
            break;
    }
}

@end
